﻿Imports CRM.BusinessLayer
Imports System.Data
Imports System.Text
Imports System.ComponentModel

Partial Public Class frmStockLetters
    Private WithEvents GBWorker As System.ComponentModel.BackgroundWorker
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrint.Click

        Dim SQL As StringBuilder
        Dim EmailAddress As String
        Dim CustType As Integer
        Dim OutputTo As Letters.OutputTypes

        Dim intStage As Integer
        Dim GBWorker As New BackgroundWorker()
        Try

            AddHandler GBWorker.DoWork, AddressOf GBWorker_DoWork
            AddHandler GBWorker.RunWorkerCompleted, AddressOf GBWorker_RunWorkerCompleted
            GBWorker.RunWorkerAsync()


            ' GlobalCRM.VirtualCompanyDir = CommonDataFunctions.getCustomizationdata("VirtualCompanyDir")

            intStage = 1
            GlobalCRM.LocalAttachments.Attachments.Clear()
            Me.Cursor = Cursors.Wait
            Me.btnPrint.IsEnabled = False

            intStage = 2

            If Me.radPrinter.IsChecked Then
                OutputTo = Letters.OutputTypes.Print
            ElseIf Me.radEmail.IsChecked Then
                OutputTo = Letters.OutputTypes.Email
            ElseIf Me.radSave.IsChecked Then
                OutputTo = Letters.OutputTypes.File
            End If
            intStage = 3

            If Me.lvwStockLetters.SelectedItems.Count = 0 Then
                CommonFuncs.DisplayMessage("Please select a letterpack first", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            intStage = 4
            If Me.chkProForma.IsChecked Then
                'check if we have an invoice address
                If GlobalCRM.CurrentCompanyID > 0 Then
                    If Not Company.HasInvoiceAddress(GlobalCRM.CurrentCompanyID) Then
                        CommonFuncs.DisplayMessage("No Invoice address has been defined for this company, cannot generate a proforma invoice", CommonDialog.CustomDialogIcons.Stop, "Error")
                        Exit Sub
                    End If
                ElseIf GlobalCRM.CurrentCandidateID > 0 Then
                    If Not People.HasInvoiceAddress(GlobalCRM.CurrentCandidateID) Then
                        CommonFuncs.DisplayMessage("No Invoice address has been defined for this person, cannot generate a proforma invoice", CommonDialog.CustomDialogIcons.Stop, "Error")
                        Exit Sub
                    End If
                End If
            End If

            intStage = 5

            If Me.radEmail.IsChecked Then
                If Me.radEmailCandidate.IsChecked Then
                    'Mail to candidate (Default)
                    EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentCandidateID)
                ElseIf Me.radEmailContact.IsChecked Then
                    'Mail to contact
                    EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentContactID)
                ElseIf Me.radEmailBoth.IsChecked Then
                    'Mail to Both
                    EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentContactID) & ";" & People.GetEmailAddress(GlobalCRM.CurrentCandidateID)
                ElseIf Me.radEmailBlank.IsChecked Then
                    'Blank
                End If
            End If

            intStage = 6

            If GlobalCRM.CurrentCompanyID > 0 Then
                CustType = 2
            Else
                CustType = 1
            End If

            intStage = 7

            'If Letters.GblFunc_Process_Letterpack(Me.lvwStockLetters.SelectedItems(0).row.itemarray(0).ToString, GlobalCRM.CurrentCaseID, 1, OutputTo, CustType) = 0 Then
            '    Exit Sub
            'End If

            intStage = 8

            If Me.chkProForma.IsChecked Then
                'Produce a proforma invoice (normal invoice based on uninvoiced charges with certain fields switched off
                Try
                    Dim CrystRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                    Dim DocstoreId As Long
                    Dim DocFileName As String

                    SQL = New StringBuilder

                    intStage = 9

                    If CustType = 2 Then
                        If Me.radEmail.IsChecked Then
                            Dim Rpt As New NormalProformaIndiv_Upd
                            'Dim Rpt As New ProformaIndiv_Upd
                            'Dim Rpt As New ProformaComp
                            Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaCompany)
                            Rpt.SetDataSource(dt)
                            If dt.Rows.Count > 0 Then
                                Rpt.SetDataSource(dt)
                                CrystRpt = Rpt
                            Else
                                CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                Exit Try
                            End If
                        End If
                        If Me.radPrinter.IsChecked Then
                            Dim Rpt As New ProformaIndiv_Upd
                            Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaCompany)
                            Rpt.SetDataSource(dt)
                            If dt.Rows.Count > 0 Then
                                Rpt.SetDataSource(dt)
                                CrystRpt = Rpt
                            Else
                                CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                Exit Try
                            End If
                        End If
                        If Me.radSave.IsChecked Then
                            If CommonFuncs.DisplayyesNoMessage("Would you like to save the proforma in Global Visas letter head?", CommonDialog.CustomDialogIcons.Question, "Proforma") = 1 Then
                                Dim Rpt As New NormalProformaIndiv_Upd
                                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaCompany)
                                Rpt.SetDataSource(dt)
                                If dt.Rows.Count > 0 Then
                                    Rpt.SetDataSource(dt)
                                    CrystRpt = Rpt
                                Else
                                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                    Exit Try
                                End If
                            Else
                                Dim Rpt As New ProformaIndiv_Upd

                                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaCompany)
                                Rpt.SetDataSource(dt)
                                If dt.Rows.Count > 0 Then
                                    Rpt.SetDataSource(dt)
                                    CrystRpt = Rpt
                                Else
                                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                    Exit Try
                                End If
                            End If

                        End If
                    Else
                        If Me.radEmail.IsChecked Then
                            'Dim Rpt As New ProformaIndiv
                            Dim Rpt As New NormalProformaIndiv_Upd
                            Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaIndividual)
                            If dt.Rows.Count > 0 Then
                                Rpt.SetDataSource(dt)
                                CrystRpt = Rpt
                            Else
                                CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                Exit Try
                            End If
                        End If
                        If Me.radPrinter.IsChecked Then
                            'Dim Rpt As New NormalProformaIndiv
                            Dim Rpt As New ProformaIndiv_Upd
                            Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaIndividual)
                            If dt.Rows.Count > 0 Then
                                Rpt.SetDataSource(dt)
                                CrystRpt = Rpt
                            Else
                                CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                Exit Try
                            End If
                        End If
                        If Me.radSave.IsChecked Then
                            If CommonFuncs.DisplayyesNoMessage("Would you like to save the proforma in Global Visas letter head?", CommonDialog.CustomDialogIcons.Question, "Proforma") = 1 Then
                                'Dim Rpt As New ProformaIndiv
                                Dim Rpt As New NormalProformaIndiv_Upd
                                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaIndividual)
                                Rpt.SetDataSource(dt)
                                If dt.Rows.Count > 0 Then
                                    Rpt.SetDataSource(dt)
                                    CrystRpt = Rpt
                                Else
                                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                    Exit Try
                                End If
                            Else
                                'Dim Rpt As New NormalProformaIndiv
                                Dim Rpt As New ProformaIndiv_Upd
                                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaIndividual)
                                Rpt.SetDataSource(dt)
                                If dt.Rows.Count > 0 Then
                                    Rpt.SetDataSource(dt)
                                    CrystRpt = Rpt
                                Else
                                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
                                    Exit Try
                                End If
                            End If

                        End If
                    End If


                    If Me.radPrinter.IsChecked Then
                        intStage = 10
                        'goes in a docstore by itself as no way to combine it with letterpack store.

                        DocstoreId = Letters.GblFunc_Create_DocStore("Proforma Invoice", "Printed", GlobalCRM.CurrentCaseID)

                        DocFileName = "C:\Database\Proforma_CaseID_" & GlobalCRM.CurrentCaseID & ".doc"

                        CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, DocFileName)

                        Letters.GblFunc_Add_FileToStore(DocFileName, "Proforma", DocstoreId)

                        Kill(DocFileName)
                        intStage = 11

                    ElseIf Me.radEmail.IsChecked Then
                        'Stick it on the C: drive to be e-mailed and put a value in local attachments
                        intStage = 12
                        DocFileName = "c:\database\Proforma_CaseID_" & GlobalCRM.CurrentCaseID & ".pdf"

                        CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, DocFileName)

                        'Letters.GblFunc_Add_FileToStore(DocFileName, "Proforma", DocstoreId)

                        GlobalCRM.LocalAttachments.AddAttachment("Proforma for case no. " & GlobalCRM.CurrentCaseID, DocFileName)

                        intStage = 13

                    ElseIf Me.radSave.IsChecked Then
                        'Stick it on the H: drive
                        intStage = 14
                        DocFileName = "H:\Proforma_CaseNo_" & GlobalCRM.CurrentCaseID & ".pdf"
                        'oWordAppl = New Word.ApplicationClass

                        'Dim test As New WordAppl

                        'Dim TemplatePath As String = "C:\Database\crm2\a.dot"

                        'Dim DocumentPath As String = DocFileName

                        'test.Open(TemplatePath)

                        'test.FindAndReplacement("<<company>>", "JAYASEELAN")

                        'test.SaveAs(DocFileName)

                        'test.Quit()

                        'CrystRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.NoFormat)

                        CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, DocFileName)
                        'CrystRpt.SaveAs(DocFileName)
                        'Dim oDoc As Word.Document
                        'Dim range As Word.Range

                        'oDoc = GetObject("C:\Documents and Settings\v\Desktop\ss.doc")
                        'oDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.InlineShapes.AddPicture("C:\Documents and Settings\v\Desktop\l.JPG")
                        'oDoc.SaveAs(FileName:=DocFileName)

                        'Dim exportOpts As New CrystalDecisions.Shared.ExportOptions 'export options for report
                        'Dim diskOpts As New CrystalDecisions.Shared.DiskFileDestinationOptions 'saving location options
                        'exportOpts = CrystRpt.ExportOptions

                        '' Set the export format.
                        'exportOpts.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.WordForWindows
                        'exportOpts.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile

                        '' Set the disk file options.
                        'diskOpts.DiskFileName = "C:\Database\crm2\a.dot" 'specify template file here!
                        'exportOpts.DestinationOptions = diskOpts

                        '' Export the report.
                        'CrystRpt.Export() 'rpt1 is the name of my Crystal Report

                    End If

                    intStage = 15
                    'done with the proforma
                Catch ex As Exception
                    CommonFuncs.ErrorHandler(ex, "frmStockLetters_ProformaError")
                    CommonFuncs.DisplayMessage("Proforma Error. Please check your crystal reports", CommonDialog.CustomDialogIcons.Information, "Error")
                End Try
            End If

            If Me.radEmail.IsChecked Then
                intStage = 16
                Me.Close()
                Dim frmSendEmail As New frmSendEmail
                With frmSendEmail
                    '.InitialiseForm(GlobalCRM.CurrentCaseID, frmSendEmail.DataTypes.VisaCase, frmSendEmail.EmailType.Candidate)
                    ' .MdiParent = frmMDIMain
                    If radEmailCandidate.IsChecked Then
                        frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Candidate
                    ElseIf radEmailContact.IsChecked Then
                        frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Contact
                    ElseIf radEmailBoth.IsChecked Then
                        frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Both
                    ElseIf radEmailBlank.IsChecked Then
                        frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Blank
                    End If
                    .Show()
                End With

                'DoCmd.OpenForm("Fm_Sendmail")

                'If StrEmail = "" Then
                '    Call MsgBox("No E-Mail address has been set up for this person, you will have to enter it by hand", , "Warning")
                'Else
                '    Form_Fm_SendMail.Txt_AddressTo = StrEmail
                'End If

                'Form_Fm_SendMail.Txt_EventType = "Stock Letter E-Mailed"
                'Form_Fm_SendMail.Txt_EventMessage = Me.List_Letterpacks.Column(1) & " letter e-mailed "

                'Form_Fm_SendMail.Txt_DataID = LngCaseID
                'Form_Fm_SendMail.Txt_DataType = 1

                'DoCmd.Close(acForm, "Fm_Stockletters")
                'Form_Fm_SendMail.SetFocus()
                'DoCmd.Maximize()

            Else
                'StrMsg = Me.List_Letterpacks.Column(1) & " letter "

                'Select Case Me.Frm_Output_to

                '    Case 1
                '        StrMsg = StrMsg & "printed"

                '    Case 2
                '        'Cant get here

                '    Case 3
                '        StrMsg = StrMsg & "saved to H: drive"

                'End Select

                'If Me.Chk_Proforma = True Then StrMsg = StrMsg & vbCrLf & "Proforma included"


                'Call GblFunc_Add_Event("Letter Printed", StrMsg, 1, Form_Fm_MainDisplay.List_Case_Cases, 1)
            End If
            intStage = 17
            Me.Cursor = Cursors.Arrow



            'intStage = 1

            'GlobalCRM.LocalAttachments.Attachments.Clear()
            'Me.Cursor = Cursors.Wait
            'Me.btnPrint.IsEnabled = False

            'intStage = 2

            'If Me.radPrinter.IsChecked Then
            '    OutputTo = Letters.OutputTypes.Print
            'ElseIf Me.radEmail.IsChecked Then
            '    OutputTo = Letters.OutputTypes.Email
            'ElseIf Me.radSave.IsChecked Then
            '    OutputTo = Letters.OutputTypes.File
            'End If
            'intStage = 3

            'If Me.lvwStockLetters.SelectedItems.Count = 0 Then
            '    CommonFuncs.DisplayMessage("Please select a letterpack first", CommonDialog.CustomDialogIcons.Information, "Error")
            '    Exit Sub
            'End If

            'intStage = 4
            'If Me.chkProForma.IsChecked Then
            '    'check if we have an invoice address
            '    If GlobalCRM.CurrentCompanyID > 0 Then
            '        If Not Company.HasInvoiceAddress(GlobalCRM.CurrentCompanyID) Then
            '            CommonFuncs.DisplayMessage("No Invoice address has been defined for this company, cannot generate a proforma invoice", CommonDialog.CustomDialogIcons.Information, "Error")
            '            Exit Sub
            '        End If
            '    ElseIf GlobalCRM.CurrentCandidateID > 0 Then
            '        If Not People.HasInvoiceAddress(GlobalCRM.CurrentCandidateID) Then
            '            CommonFuncs.DisplayMessage("No Invoice address has been defined for this person, cannot generate a proforma invoice", CommonDialog.CustomDialogIcons.Information, "Error")
            '            Exit Sub
            '        End If
            '    End If
            'End If

            'intStage = 5

            'If Me.radEmail.IsChecked Then
            '    If Me.radEmailCandidate.IsChecked Then
            '        'Mail to candidate (Default)
            '        EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentCandidateID)
            '    ElseIf Me.radEmailContact.IsChecked Then
            '        'Mail to contact
            '        EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentContactID)
            '    ElseIf Me.radEmailBoth.IsChecked Then
            '        'Mail to Both
            '        EmailAddress = People.GetEmailAddress(GlobalCRM.CurrentContactID) & ";" & People.GetEmailAddress(GlobalCRM.CurrentCandidateID)
            '    ElseIf Me.radEmailBlank.IsChecked Then
            '        'Blank
            '    End If
            'End If

            'intStage = 6

            'If GlobalCRM.CurrentCompanyID > 0 Then
            '    CustType = 2
            'Else
            '    CustType = 1
            'End If

            'intStage = 7

            'If Letters.GblFunc_Process_Letterpack(Me.lvwStockLetters.SelectedItem.row.itemarray(0).ToString, GlobalCRM.CurrentCaseID, 1, OutputTo, CustType) = 0 Then
            '    Exit Sub
            'End If

            'intStage = 8

            'If Me.chkProForma.IsChecked Then
            '    'Produce a proforma invoice (normal invoice based on uninvoiced charges with certain fields switched off
            '    Try
            '        Dim CrystRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            '        Dim DocstoreId As Long
            '        Dim DocFileName As String

            '        SQL = New StringBuilder

            '        intStage = 9

            '        If CustType = 2 Then
            '            If Me.radEmail.IsChecked Then
            '                Dim Rpt As New ProformaComp
            '                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaCompany)
            '                Rpt.SetDataSource(dt)
            '                If dt.Rows.Count > 0 Then
            '                    Rpt.SetDataSource(dt)
            '                    CrystRpt = Rpt
            '                Else
            '                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                    Exit Try
            '                End If
            '            End If
            '            If Me.radPrinter.IsChecked Then
            '                Dim Rpt As New NormalProformaComp
            '                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaCompany)
            '                Rpt.SetDataSource(dt)
            '                If dt.Rows.Count > 0 Then
            '                    Rpt.SetDataSource(dt)
            '                    CrystRpt = Rpt
            '                Else
            '                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                    Exit Try
            '                End If
            '            End If
            '            If Me.radSave.IsChecked Then
            '                If MsgBox("Would you like to save the proforma in Global Visas letter head?", MsgBoxStyle.YesNo, "Proforma") = MsgBoxResult.Yes Then
            '                    Dim Rpt As New ProformaComp
            '                    Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaCompany)
            '                    Rpt.SetDataSource(dt)
            '                    If dt.Rows.Count > 0 Then
            '                        Rpt.SetDataSource(dt)
            '                        CrystRpt = Rpt
            '                    Else
            '                        CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                        Exit Try
            '                    End If
            '                Else
            '                    Dim Rpt As New NormalProformaComp
            '                    Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaCompany)
            '                    Rpt.SetDataSource(dt)
            '                    If dt.Rows.Count > 0 Then
            '                        Rpt.SetDataSource(dt)
            '                        CrystRpt = Rpt
            '                    Else
            '                        CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                        Exit Try
            '                    End If
            '                End If

            '            End If
            '        Else
            '            If Me.radEmail.IsChecked Then
            '                Dim Rpt As New ProformaIndiv
            '                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaIndividual)
            '                If dt.Rows.Count > 0 Then
            '                    Rpt.SetDataSource(dt)
            '                    CrystRpt = Rpt
            '                Else
            '                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                    Exit Try
            '                End If
            '            End If
            '            If Me.radPrinter.IsChecked Then
            '                Dim Rpt As New NormalProformaIndiv
            '                Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaIndividual)
            '                If dt.Rows.Count > 0 Then
            '                    Rpt.SetDataSource(dt)
            '                    CrystRpt = Rpt
            '                Else
            '                    CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                    Exit Try
            '                End If
            '            End If
            '            If Me.radSave.IsChecked Then
            '                If MsgBox("Would you like to save the proforma in Global Visas letter head?", MsgBoxStyle.YesNo, "Proforma") = MsgBoxResult.Yes Then
            '                    Dim Rpt As New ProformaIndiv
            '                    Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.ProformaIndividual)
            '                    Rpt.SetDataSource(dt)
            '                    If dt.Rows.Count > 0 Then
            '                        Rpt.SetDataSource(dt)
            '                        CrystRpt = Rpt
            '                    Else
            '                        CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                        Exit Try
            '                    End If
            '                Else
            '                    Dim Rpt As New NormalProformaIndiv
            '                    Dim dt As DataTable = ReportCRM.GetReportData(ReportCRM.ReportType.NormalProformaIndividual)
            '                    Rpt.SetDataSource(dt)
            '                    If dt.Rows.Count > 0 Then
            '                        Rpt.SetDataSource(dt)
            '                        CrystRpt = Rpt
            '                    Else
            '                        CommonFuncs.DisplayMessage("No data in Proforma. Hence Performa is not generated. Please contact IT for more information", CommonDialog.CustomDialogIcons.Information, "Error")
            '                        Exit Try
            '                    End If
            '                End If

            '            End If
            '        End If


            '        If Me.radPrinter.IsChecked Then
            '            intStage = 10
            '            'goes in a docstore by itself as no way to combine it with letterpack store.

            '            DocstoreId = Letters.GblFunc_Create_DocStore("Proforma Invoice", "Printed", GlobalCRM.CurrentCaseID)

            '            DocFileName = "C:\Database\Proforma_CaseID_" & GlobalCRM.CurrentCaseID & ".doc"

            '            CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, DocFileName)

            '            Letters.GblFunc_Add_FileToStore(DocFileName, "Proforma", DocstoreId)

            '            Kill(DocFileName)
            '            intStage = 11

            '        ElseIf Me.radEmail.IsChecked Then
            '            'Stick it on the C: drive to be e-mailed and put a value in local attachments
            '            intStage = 12
            '            DocFileName = "c:\database\Proforma_CaseID_" & GlobalCRM.CurrentCaseID & ".doc"

            '            CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, DocFileName)

            '            'Letters.GblFunc_Add_FileToStore(DocFileName, "Proforma", DocstoreId)

            '            GlobalCRM.LocalAttachments.AddAttachment("Proforma for case no. " & GlobalCRM.CurrentCaseID, DocFileName)

            '            intStage = 13

            '        ElseIf Me.radSave.IsChecked Then
            '            'Stick it on the H: drive
            '            intStage = 14
            '            DocFileName = "H:\Proforma_CaseNo_" & GlobalCRM.CurrentCaseID & ".pdf"
            '            'oWordAppl = New Word.ApplicationClass

            '            'Dim test As New WordAppl



            '            'Dim DocumentPath As String = DocFileName

            '            'test.Open(TemplatePath)

            '            'test.FindAndReplacement("<<company>>", "JAYASEELAN")

            '            'test.SaveAs(DocFileName)

            '            'test.Quit()

            '            'CrystRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.NoFormat)

            '            CrystRpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, DocFileName)
            '            'CrystRpt.SaveAs(DocFileName)
            '            'Dim oDoc As Word.Document
            '            'Dim range As Word.Range



            '            'oDoc.SaveAs(FileName:=DocFileName)

            '            'Dim exportOpts As New CrystalDecisions.Shared.ExportOptions 'export options for report
            '            'Dim diskOpts As New CrystalDecisions.Shared.DiskFileDestinationOptions 'saving location options
            '            'exportOpts = CrystRpt.ExportOptions

            '            '' Set the export format.
            '            'exportOpts.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.WordForWindows
            '            'exportOpts.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile

            '            '' Set the disk file options.

            '            'exportOpts.DestinationOptions = diskOpts

            '            '' Export the report.
            '            'CrystRpt.Export() 'rpt1 is the name of my Crystal Report

            '        End If

            '        intStage = 15
            '        'done with the proforma
            '    Catch ex As Exception
            '        'CommonFunc.ErrorHandler(ex, "frmStockLetters_ProformaError")
            '        CommonFuncs.DisplayMessage("Proforma Error. Please check your crystal reports", CommonDialog.CustomDialogIcons.Information, "Error")
            '    End Try
            'End If

            'If Me.radEmail.IsChecked Then
            '    intStage = 16
            '    Me.Close()
            '    Dim frmSendEmail As New frmSendEmail
            '    With frmSendEmail
            '        '.InitialiseForm(GlobalCRM.CurrentCaseID, frmSendEmail.DataTypes.VisaCase, frmSendEmail.EmailType.Candidate)
            '        ' .MdiParent = frmMDIMain
            '        If radEmailCandidate.IsChecked Then
            '            frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Candidate
            '        ElseIf radEmailContact.IsChecked Then
            '            frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Contact
            '        ElseIf radEmailBoth.IsChecked Then
            '            frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Both
            '        ElseIf radEmailBlank.IsChecked Then
            '            frmSendEmail.iEmailType = CommonDataFunctions.EmailType.Blank
            '        End If
            '        .Show()
            '    End With

            '    'DoCmd.OpenForm("Fm_Sendmail")

            '    'If StrEmail = string.Empty Then
            '    '    Call MsgBox("No E-Mail address has been set up for this person, you will have to enter it by hand", , "Warning")
            '    'Else
            '    '    Form_Fm_SendMail.Txt_AddressTo = StrEmail
            '    'End If

            '    'Form_Fm_SendMail.Txt_EventType = "Stock Letter E-Mailed"
            '    'Form_Fm_SendMail.Txt_EventMessage = Me.List_Letterpacks.Column(1) & " letter e-mailed "

            '    'Form_Fm_SendMail.Txt_DataID = LngCaseID
            '    'Form_Fm_SendMail.Txt_DataType = 1

            '    'DoCmd.Close(acForm, "Fm_Stockletters")
            '    'Form_Fm_SendMail.SetFocus()
            '    'DoCmd.Maximize()

            'Else
            '    'StrMsg = Me.List_Letterpacks.Column(1) & " letter "

            '    'Select Case Me.Frm_Output_to

            '    '    Case 1
            '    '        StrMsg = StrMsg & "printed"

            '    '    Case 2
            '    '        'Cant get here

            '    '    Case 3
            '    '        StrMsg = StrMsg & "saved to H: drive"

            '    'End Select

            '    'If Me.Chk_Proforma = True Then StrMsg = StrMsg & vbCrLf & "Proforma included"


            '    'Call GblFunc_Add_Event("Letter Printed", StrMsg, 1, Form_Fm_MainDisplay.List_Case_Cases, 1)
            'End If
            'intStage = 17
            'Me.Cursor = Cursors.Arrow
        Catch ex As Exception
            intStage = 18
            CommonFuncs.ErrorHandler(ex, "frmStockLetters.btnPrint_Click." & CStr(intStage))
        Finally
            Me.btnPrint.IsEnabled = True
            Me.Cursor = Cursors.Arrow
            GBWorker = Nothing
        End Try
    End Sub
    Private Sub GBWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        'btnPrint.IsEnabled = False
        ''Me.Cursor = Cursors.Wait
    End Sub
    Private Sub GBWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        'Me.Cursor = Cursors.Arrow
        'btnSearch.IsEnabled = True
        'btnHistory.IsEnabled = True
        btnPrint.IsEnabled = True

    End Sub
    Private Sub frmStockLetters_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmStockLetters_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim bind As New Binding()
        Dim dt As DataTable
        Try
            GeneralTasks.ChangeTheme(Me)
            GeneralTasks.EnumerateandFill(Me.LogicalChildren)
            Dim SLSId As Integer = CommonDataFunctions.GblFunc_Get_Dataset(GlobalCRM.CurrentCaseID, 3)
            Dim ClientType As Integer

            If GlobalCRM.CurrentCompanyID > 0 Then
                ClientType = 2
                'Me.grpEmail.Visible = True
            Else
                ClientType = 1
                'Me.grpEmail.Visible = False
            End If

            txtSetName.Text = SLSId & " : " & Letters.GetLetterSetName(SLSId)
            'DataBinding.BindListView(Letters.Gblfunc_Get_SLSSQL(SLSId, ClientType, Cases.GetCaseStage(GlobalCRM.CurrentCaseID).StageNumber, 1), Me.lvwStockLetters)

            dt = Letters.Gblfunc_Get_SLSSQL(SLSId, ClientType, Cases.GetCaseStage(GlobalCRM.CurrentCaseID).StageNumber, 1)
            bind.Source = dt
            lvwStockLetters.SetBinding(ListView.ItemsSourceProperty, bind)


            If GlobalCRM.CurrentCompanyID() = 0 Then
                'grpEmail.isEnabled = False
                radEmailCandidate.IsChecked = True
            Else
                radEmailContact.IsChecked = True
            End If
        Catch ex As Exception

        Finally
            dt = Nothing
            bind = Nothing
        End Try
    End Sub
    'Private Sub OutputSaveChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radEmail.isChecked ', radPrinter.isChecked, radSave.isChecked ', radEmail.Unchecked, radPrinter.Unchecked, radSave.Unchecked
    '    'If Not radEmail Is Nothing And Not radPrinter Is Nothing And Not radSave Is Nothing Then
    '    'If Me.radEmail.IsChecked Then
    '    '    Me.grpEmail.Visibility = Windows.Visibility.Visible
    '    '    Me.grpEmail.IsEnabled = True
    '    '    Me.radEmailCandidate.IsEnabled = True
    '    '    Me.radEmailContact.IsEnabled = True
    '    '    Me.radEmailBlank.IsEnabled = True
    '    '    Me.radEmailBoth.IsEnabled = True
    '    '    Me.btnPrint.Content = "Email"
    '    'ElseIf Me.radPrinter.IsChecked Then
    '    '    Me.grpEmail.IsEnabled = False
    '    '    Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '    '    Me.radEmailCandidate.IsEnabled = False
    '    '    Me.radEmailContact.IsEnabled = False
    '    '    Me.radEmailBlank.IsEnabled = False
    '    '    Me.radEmailBoth.IsEnabled = False
    '    '    Me.btnPrint.Content = "Print"
    '    If Me.radSave.IsChecked Then
    '        Me.grpEmail.IsEnabled = False
    '        Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '        Me.radEmailCandidate.IsEnabled = False
    '        Me.radEmailContact.IsEnabled = False
    '        Me.radEmailBlank.IsEnabled = False
    '        Me.radEmailBoth.IsEnabled = False
    '        Me.btnPrint.Content = "Save"
    '    End If
    '    'End If
    'End Sub
    Private Sub OutputChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radEmail.Checked, radPrinter.Checked, radSave.Checked, radEmail.Unchecked, radPrinter.Unchecked, radSave.Unchecked
        If Not radEmail Is Nothing And Not radPrinter Is Nothing And Not radSave Is Nothing Then
            If Not radEmail Is Nothing Then

                If Me.radEmail.IsChecked Then
                    Me.grpEmail.Visibility = Windows.Visibility.Visible
                    Me.grpEmail.IsEnabled = True
                    Me.radEmailCandidate.IsEnabled = True
                    Me.radEmailContact.IsEnabled = True
                    Me.radEmailBlank.IsEnabled = True
                    Me.radEmailBoth.IsEnabled = True
                    Me.btnPrint.Content = "Email"
                End If
            End If
            If Not radPrinter Is Nothing Then
                If Me.radPrinter.IsChecked Then
                    Me.grpEmail.IsEnabled = False
                    Me.grpEmail.Visibility = Windows.Visibility.Hidden
                    Me.radEmailCandidate.IsEnabled = False
                    Me.radEmailContact.IsEnabled = False
                    Me.radEmailBlank.IsEnabled = False
                    Me.radEmailBoth.IsEnabled = False
                    Me.btnPrint.Content = "Print"
                End If
            End If
            If Not radSave Is Nothing Then
                If Me.radSave.IsChecked Then
                    Me.grpEmail.IsEnabled = False
                    Me.grpEmail.Visibility = Windows.Visibility.Hidden
                    Me.radEmailCandidate.IsEnabled = False
                    Me.radEmailContact.IsEnabled = False
                    Me.radEmailBlank.IsEnabled = False
                    Me.radEmailBoth.IsEnabled = False
                    Me.btnPrint.Content = "Save"
                End If
            End If
        End If
    End Sub
    'Private Sub OutputEmailChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radEmail.isChecked
    '    ' If Not radEmail Is Nothing And Not radPrinter Is Nothing And Not radSave Is Nothing Then
    '    If Me.radEmail.IsChecked Then
    '        Me.grpEmail.Visibility = Windows.Visibility.Visible
    '        Me.grpEmail.IsEnabled = True
    '        Me.radEmailCandidate.IsEnabled = True
    '        Me.radEmailContact.IsEnabled = True
    '        Me.radEmailBlank.IsEnabled = True
    '        Me.radEmailBoth.IsEnabled = True
    '        Me.btnPrint.Content = "Email"
    '        'ElseIf Me.radPrinter.IsChecked Then
    '        '    Me.grpEmail.IsEnabled = False
    '        '    Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '        '    Me.radEmailCandidate.IsEnabled = False
    '        '    Me.radEmailContact.IsEnabled = False
    '        '    Me.radEmailBlank.IsEnabled = False
    '        '    Me.radEmailBoth.IsEnabled = False
    '        '    Me.btnPrint.Content = "Print"
    '        'ElseIf Me.radSave.IsChecked Then
    '        '    Me.grpEmail.IsEnabled = False
    '        '    Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '        '    Me.radEmailCandidate.IsEnabled = False
    '        '    Me.radEmailContact.IsEnabled = False
    '        '    Me.radEmailBlank.IsEnabled = False
    '        '    Me.radEmailBoth.IsEnabled = False
    '        '    Me.btnPrint.Content = "Save"
    '        'End If
    '    End If
    'End Sub
    'Private Sub OutputPrintChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radPrinter.isChecked
    '    'If Not radEmail Is Nothing And Not radPrinter Is Nothing And Not radSave Is Nothing Then
    '    '    If Me.radEmail.IsChecked Then
    '    '        Me.grpEmail.Visibility = Windows.Visibility.Visible
    '    '        Me.grpEmail.IsEnabled = True
    '    '        Me.radEmailCandidate.IsEnabled = True
    '    '        Me.radEmailContact.IsEnabled = True
    '    '        Me.radEmailBlank.IsEnabled = True
    '    '        Me.radEmailBoth.IsEnabled = True
    '    '        Me.btnPrint.Content = "Email"
    '    '    Else
    '    If Me.radPrinter.IsChecked Then
    '        Me.grpEmail.IsEnabled = False
    '        Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '        Me.radEmailCandidate.IsEnabled = False
    '        Me.radEmailContact.IsEnabled = False
    '        Me.radEmailBlank.IsEnabled = False
    '        Me.radEmailBoth.IsEnabled = False
    '        Me.btnPrint.Content = "Print"
    '        'ElseIf Me.radSave.IsChecked Then
    '        '    Me.grpEmail.IsEnabled = False
    '        '    Me.grpEmail.Visibility = Windows.Visibility.Hidden
    '        '    Me.radEmailCandidate.IsEnabled = False
    '        '    Me.radEmailContact.IsEnabled = False
    '        '    Me.radEmailBlank.IsEnabled = False
    '        '    Me.radEmailBoth.IsEnabled = False
    '        '    Me.btnPrint.Content = "Save"
    '        'End If
    '    End If
    'End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()

    End Sub


End Class
